﻿using Kingdee.BOS;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.Core;
using Kingdee.BOS.JSON;
using Kingdee.BOS.Log;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.WebApi.Client;
using Kingdee.BOS.WebApi.DataEntities;
using Kingdee.BOS.WebApi.FormService;
using System;
using System.IO;
using System.Net;

namespace Caraya.MFG.BillPlugIn
{
    /// <summary>
    /// 执行计划
    /// Caraya.MFG.BillPlugIn.PurPdfService,Caraya.MFG.BillPlugIn
    /// </summary>
    public class PurPdfService : IScheduleService
    {
        public void Run(Context ctx, Schedule schedule)
        {
            string formId = schedule.ParameterFormId;
            string param1 = schedule.Parameters;
            try
            {
                var strSQL = "SELECT * FROM T_BAS_ATTACHMENT WHERE FBILLTYPE='PUR_PurchaseOrder'";
                var attaList = DBServiceHelper.ExecuteDynamicObject(ctx, strSQL);
                var pdfUrl = new Uri("https://wx.g127.com:9008/UploadFiles/background.jpg");
                var pdfName = "1.jpg";
                WebRequest request = WebRequest.Create(pdfUrl);
                using (WebResponse response = request.GetResponse())
                {
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        using (MemoryStream memoryStream = new MemoryStream())
                        {
                            responseStream.CopyTo(memoryStream);
                            byte[] fileData = memoryStream.ToArray();
                            string base64EncodedData = Convert.ToBase64String(fileData);
                            // 在此处使用 base64EncodedData 进行其他操作
                            var AttObject = new JSONObject();
                            AttObject.Put("FileName", pdfName);
                            AttObject.Put("FormId", "PUR_PurchaseOrder");
                            AttObject.Put("IsLast", true);
                            AttObject.Put("InterId", 100005);
                            AttObject.Put("BillNO", "CGDD000003");
                            AttObject.Put("AliasFileName", pdfName);
                            AttObject.Put("SendByte", base64EncodedData);
                            //var result = WebApiServiceCall.AttachmentUpload(ctx, AttObject.ToJSONString());
                            var webapi = new K3CloudApi();
                            webapi.InitClient("61271b176f110c", "217311_Q6dO67sGTmmUQ5TKT27q7yXvzq2U5AOP", "b53e78db1bae4f859e2acb0de4d52552", "test", 2052, "100", "http://localhost:1200/k3cloud/");
                            var result = webapi.Execute<Repo>("Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.AttachmentUpLoad", new object[] { AttObject });
                            if (!result.Result.ResponseStatus.IsSuccess)
                            {
                                throw new Exception(result.Result.ResponseStatus.Errors[0].Message);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error("PurPdfService", ex.Message, ex);
            }
        }
    }
}
